package com.example.ruralservicedigitalplatform.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.ruralservicedigitalplatform.constant.MessageConstant;
import com.example.ruralservicedigitalplatform.domain.dto.VillagerDto;
import com.example.ruralservicedigitalplatform.domain.dto.VillagerEditDto;
import com.example.ruralservicedigitalplatform.domain.dto.VillagerLoginDto;
import com.example.ruralservicedigitalplatform.domain.po.Villager;
import com.example.ruralservicedigitalplatform.domain.vo.VillagerLoginVO;
import com.example.ruralservicedigitalplatform.exception.LoginFailedException;
import com.example.ruralservicedigitalplatform.mapper.VillagerMapper;

import com.example.ruralservicedigitalplatform.properties.WeChatProperties;
import com.example.ruralservicedigitalplatform.service.VillagerService;
import com.example.ruralservicedigitalplatform.utils.HttpClientUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class VillagerServiceImpl implements VillagerService {
    //微信服务接口地址
    public static final String WX_LOGIN = "https://api.weixin.qq.com/sns/jscode2session";
    @Autowired
    private WeChatProperties weChatProperties;
    @Autowired
    private VillagerMapper villagerMapper;
    /**
     * 微信登录
     * @param villagerLoginDto
     * @return
     */
    @Transactional
    @Override
    public Villager wxLogin(VillagerLoginDto villagerLoginDto) {
        String openid = getOpenId(villagerLoginDto.getCode());

        //判断openid是否为空，如果为空登录失败，抛出业务异常
        if (openid == null) {
            throw new LoginFailedException(MessageConstant.LOGIN_FAILED);
        }
        //判断当前用户是否为新用户
        Villager villager = villagerMapper.getById(openid);
        //如果是，自动完成注册
        if (villager == null) {
            villager = Villager.builder()
                    .openid(openid)
                    .createTime(LocalDateTime.now())
                    .build();
            villagerMapper.insert(villager);
        }
        //返回用户对象

        return villager;

    }
    /**
     * 调用微信接口服务，获得当前微信用户的openid
     * @param code
     * @return
     */
    private String getOpenId(String code){
        Map<String, String> map = new HashMap<>();
        map.put("appid", weChatProperties.getAppid());
        map.put("secret", weChatProperties.getSecret());
        map.put("js_code", code);
        map.put("grant_type", "authorization_code");
        String json = HttpClientUtil.doGet(WX_LOGIN, map);

        JSONObject jsonObject = JSON.parseObject(json);
        String openid = jsonObject.getString("openid");
        return openid;
    }
//    /**
//     * 村民登录
//     * @param villagerDto
//     * @return
//     */
//    @Override
//    public Villager login(VillagerDto villagerDto) {
//        return villagerMapper.getByUsernameAndPassword(villagerDto);
//    }
//    /**
//     * 村民注册
//     * @param villagerDto
//     */
//    @Override
//    public void register(VillagerDto villagerDto) {
//        Villager villager=new Villager();
//        BeanUtils.copyProperties(villagerDto,villager);
//
//        villager.setCreateTime(LocalDateTime.now());
//
//        villager.setUpdateTime(LocalDateTime.now());
//
//
//        villagerMapper.register(villager);
//
//    }
//    /**
//     * 编辑信息
//     * @param villagerEditDto
//     */
//    @Override
//    public void update(VillagerEditDto villagerEditDto) {
//            Villager villager = new Villager();
//            BeanUtils.copyProperties(villagerEditDto,villager);
//            villager.setUpdateTime(LocalDateTime.now());
//            villagerMapper.update(villager);
//
//
//    }
//
//    /**
//     * 查询信息
//     * @return
//     */
//    @Override
//    public VillagerVo selectById(Long id) {
//        VillagerVo villager=villagerMapper.selectById(id);
//        //VillagerVO villager = villagerMapper.select();
//        //villager.setPassword("******");
//        return villager;
//    }


}